package com.ztom.beans.factory.support.extenssions

import com.ztom.beans.exception.BeansException
import com.ztom.beans.factory.BeanFactory
import java.util.*

/**
 * 根据 beanName 和 type 获取 bean
 */
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline fun <reified T> BeanFactory.getBean(beanName: String): T {
    val bean = this.getBean(beanName)
    return if (bean is T) bean else
        throw BeansException("no bean named [$beanName] is type of [${T::class.qualifiedName}]")
}

/**
 * 根据 type 获取 bean
 */
inline fun <reified T> BeanFactory.getBean(): T {
    // 默认处理为首字母小写
    val beanName = T::class.simpleName!!.replaceFirstChar { it.lowercase(Locale.getDefault()) }
    return this.getBean<T>(beanName)
}